#include "DownloadCaseThread.h"

#include "../Global/ECGGlobal.h"
#include "../ali/OSS.h"
#include "../Tools/alert.h"


#include <QDir>
#include <QFileInfo>
#include <QStringList>

#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>

#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

#include <QDebug>
#include <QDateTime>

DownloadCaseThread::DownloadCaseThread()
{
	m_nProgress = 0;
	errCode = 0;
}

DownloadCaseThread::~DownloadCaseThread()
{

}

void DownloadCaseThread::run()
{
	errCode = 0;

	for (QString s : downloadList){
		qDebug() << "[DownloadList]" << s;
		if (s.isEmpty())
			continue;

		QString ext = s.mid(s.lastIndexOf("."));
		QString localPath = dirName + "\\" + baseName + ext;

		QFile file;
		file.setFileName(localPath);
		if (file.exists())
			continue;

		QByteArray byte = InitGetRequest(s, "DownloadFileFromUrl");
		if (byte.isEmpty()){
			errorList.push_back(s);
		}
		else{
			if (file.open(QIODevice::WriteOnly)){
				file.write(byte);
				file.close();
			}
		}
	}

	m_nProgress = 100;
}